package com.phaeton.beemeup;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.ZoomCamera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.level.LevelLoader;
import org.andengine.util.texturepack.TexturePackTextureRegionLibrary;

import android.util.Log;
import android.view.KeyEvent;

import com.phaeton.beemeup.SceneManager.SceneType;

public class BeeMeUpActivity extends BaseGameActivity implements IOnMenuItemClickListener {

	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 800; 
	private static final int CAMERA_HEIGHT = 480; 
	
	private static int LEVEL_WIDTH; 
	private static int LEVEL_HEIGHT; 

	private static final int LAYER_COUNT = 3;
	
	// ===========================================================
	// Fields
	// ===========================================================

	private ZoomCamera mZoomCamera;
	
	private GameManager mGameManager;
	
	
	
	// ===========================================================
	// Constructors
	// ===========================================================
	private TexturePackTextureRegionLibrary mSpritesheetTexturePackTextureRegionLibrary;
	private SceneManager mSceneManager;
	private LevelLoader mLevelLoader;
	private Camera mCamera;

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	
	@Override
	public EngineOptions onCreateEngineOptions() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		this.mZoomCamera = new ZoomCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		this.mZoomCamera.setBounds(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		this.mZoomCamera.setBoundsEnabled(true);
		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mZoomCamera);
    }

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {

		mSceneManager = new SceneManager(this, mCamera);
		mSceneManager.loadSplashSceneResources();
		
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		pOnCreateSceneCallback.onCreateSceneFinished(mSceneManager.createSplashScene());
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		mEngine.registerUpdateHandler(new TimerHandler(1f, new ITimerCallback() {
	      public void onTimePassed(final TimerHandler pTimerHandler)
	      {
	         mEngine.unregisterUpdateHandler(pTimerHandler);
	         mSceneManager.loadGameSceneResources();
	         mSceneManager.createTitleScene();
	         mSceneManager.createLevelMenuScene();
	         mSceneManager.setCurrentScene(SceneType.TITLE);	 		
	 		 mGameManager = new GameManager(BeeMeUpActivity.this, mZoomCamera);
	 		 mGameManager.init();
	      }
	   }));
		
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

	//spawn new bees
	
	
	//Menu
	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch(pMenuItem.getID()) {
//			case 1:
//				/* Restart the animation. */
//				this.mMainScene.reset();
//	
//				/* Remove the menu and reset it. */
//				this.mMainScene.clearChildScene();
//				this.mMenuScene.reset();
//				return true;
//			case 2:
//				/* End Activity. */
//				this.finish();
//				return true;
			default:
				return false;
		}
	}
	
	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
		Log.e("BeeKeys", KeyEvent.KEYCODE_MENU + ": " + pKeyCode + " " + pEvent);
		if(pKeyCode == KeyEvent.KEYCODE_MENU && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
			if(mSceneManager.getCurrentScene() == SceneType.GAME) {
				mSceneManager.setCurrentScene(SceneType.TITLE);	 	
			} 
			return true;
		} else if(pKeyCode == KeyEvent.KEYCODE_BACK && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
			if(mSceneManager.getCurrentScene() == SceneType.TITLE) {
				return super.onKeyDown(pKeyCode, pEvent);
			} 
			if(mSceneManager.getCurrentScene() == SceneType.GAME) {
				mGameManager.unloadLevel();
				mSceneManager.setCurrentScene(SceneType.LEVELMENU);	 	
			} 
			if(mSceneManager.getCurrentScene() == SceneType.LEVELMENU) {
				mSceneManager.setCurrentScene(SceneType.TITLE);	 	
			} 
			return true;
		} else {
			return super.onKeyDown(pKeyCode, pEvent);
		}
	}
	
	public SceneManager getSceneManager(){
		return mSceneManager;
	}
	
	public GameManager getGameManager(){
		return mGameManager;
	}
}